#include "Subscriber.h"
#include <algorithm>

CSubscriberInfo::CSubscriberInfo()
{
    init();
}

CSubscriberInfo::~CSubscriberInfo()
{
}

void CSubscriberInfo::init()
{
    m_topic = "";
    m_clientMap.clear();
}

std::string CSubscriberInfo::getTopicName()
{
    return m_topic;
}

void CSubscriberInfo::setTopicName(const std::string &topic)
{
    m_topic = topic;
}

std::map<int, ClientInfo> CSubscriberInfo::getClientMap()
{
    return m_clientMap;
}
std::vector<int> CSubscriberInfo::getClientIdList()
{
    std::vector<int> keys;
    for (const auto &pair : m_clientMap)
    {
        keys.push_back(pair.first); // pair.first是键，pair.second是值
    }
    return keys;
}
void CSubscriberInfo::clientIdAdd(const int id)
{
    m_clientMap.insert(std::make_pair(id, ClientInfo(DEFAULT_RATE))); // 直接添加，有则更新
}
void CSubscriberInfo::clientIdAdd(const int id, const int rate)
{
    m_clientMap.insert(std::make_pair(id, ClientInfo(rate))); // 直接添加，有则更新
}
void CSubscriberInfo::clientIdRemove(const int id)
{
    if (m_clientMap.find(id) != m_clientMap.end())
    {
        m_clientMap.erase(id);
    }
}
void CSubscriberInfo::clientMapClear()
{
    m_clientMap.clear();
}

int CSubscriberInfo::getSendRate(const int id)
{
    int rate = DEFAULT_RATE;
    if (m_clientMap.find(id) != m_clientMap.end())
    {
        rate = m_clientMap.at(id).rate;
    }
    return rate;
}
void CSubscriberInfo::setSendRate(const int id, const int rate)
{
    if (m_clientMap.find(id) != m_clientMap.end())
    {
        m_clientMap[id].rate = rate < 1 ? 1 : rate; // 最小是1
    }
}

TimePoint CSubscriberInfo::getSendTime(const int id)
{
    TimePoint time = HighResClock::now();
    if (m_clientMap.find(id) != m_clientMap.end())
    {
        time = m_clientMap.at(id).sendTime;
    }
    return time;
}
void CSubscriberInfo::setSendTime(const int id, const TimePoint time)
{
    if (m_clientMap.find(id) != m_clientMap.end())
    {
        m_clientMap[id].sendTime = time;
    }
}